package com.dongni.tools;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * 全局通用类
 * @autor hongbing
 * @date 2018/6/13
 */
public final class Common {

    private static Context sContext;
    private static boolean debug = false;
    // 保存在手机里面的文件名
    private static String SP_FILE_NAME;

    public static void init(Context context) {
        sContext = context;
    }

    public static void init(Context context, boolean debug) {
        init(context);
        Common.debug = debug;
    }

    public static void init(Context context, String spFileName, boolean debug) {
        init(context, debug);
        SP_FILE_NAME = spFileName;
    }

    public static Context getContext() {
        return sContext;
    }

    public static void setDebug(boolean debug) {
        Common.debug = debug;
    }

    public static String getSpFileName(){
        return SP_FILE_NAME;
    }

    /**
     * 获取当前使用的sdk版本
     * @return
     */
    public static int getSdkInt() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * 获取手机系统版本号
     * @return
     */
    public static String getSystemVersion(){
        return Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     * @return
     */
    public static String getMobileType(){
        return Build.MODEL;
    }

    /**
     * 获取手机厂商
     * @return
     */
    public static String getMobileBrand(){
        return Build.BRAND;
    }

    /**
     * 获取浏览器的ua
     * @return
     */
    public static String getBrowserUa(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return WebSettings.getDefaultUserAgent(sContext);
        }else{
            WebView webView = new WebView(sContext);
            WebSettings settings = webView.getSettings();
            return settings.getUserAgentString();
        }
    }

    /**
     * 获取应用版本名
     * @return
     */
    public static String getVersionName() {
        if (sContext == null) {
            return "";
        }
        String versionName = "";
        PackageManager pManager = sContext.getPackageManager();
        try {
            PackageInfo info = pManager.getPackageInfo(sContext.getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            versionName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 获取应用版本号
     * @return
     */
    public static int getVersionCode() {
        if (sContext == null) {
            return 0;
        }
        int versionCode = 0;
        PackageManager pManager = sContext.getPackageManager();
        try {
            PackageInfo info = pManager.getPackageInfo(sContext.getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            versionCode = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    private static String getUa(String browerUa){
        StringBuilder sBuilder = new StringBuilder("android/");
        sBuilder.append(Common.getVersionName() + "|");
        sBuilder.append(Common.getMobileBrand() + "/");
        sBuilder.append(Common.getMobileType() + "|");
        sBuilder.append(browerUa + "|");
        sBuilder.append("Okhttp 1.1");
        return sBuilder.toString();
    }

    /**
     * 判断当前设备是手机还是平板，代码来自 Google I/O App for Android
     * @param context
     * @return 平板返回 True，手机返回 False
     */
    public static boolean isPad(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    public static void log_d(String tag, String msg) {
        if (debug) {
            Log.d(tag, msg);
        }
    }

    public static void log_e(String tag, String msg) {
        if (debug) {
            Log.d(tag, msg);
        }
    }
}
